(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:BalancelogCtrl
     * @description
     * # BalancelogCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('BalancelogCtrl', BalancelogCtrl);

    BalancelogCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location'];

    function BalancelogCtrl($scope, dataService, helper, $state, $location) {

        $scope.listAction = function() {
            $scope.page_Size = 10;
            $scope.chosedStatus = "zengjia";

            $scope.getlist(1);
        }

        $scope.getlist = function(pageNo, member_level_id) {
            var query = { 
                page_no: pageNo ? pageNo : "1", 
                page_size: $scope.page_Size, 
                order_by: "id:desc",
                query: {} 
            };

            if (member_level_id) {
                query.query = { member_level_id: member_level_id };
            }

            if ($scope.chosedStatus) {
                if ($scope.chosedStatus == "zengjia") {
                    query.query.kind = 2;
                }

                if ($scope.chosedStatus == "jianshao") {
                    query.query.kind = 3;
                }
            }

            //查询已支付的充值订单
            query.query.state = 1;

            dataService.balancelogGet(query).then(function(list) {
                if (list.balance_logs) {
                    if ($scope.id) {
                        $scope.balance_log = list.balance_logs[0];
                    } else {
                        $scope.balance_logs = list.balance_logs;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        $scope.infoAction = function() {
            $scope.id = $location.search().id;
            if ($scope.id) {
                $scope.getlist(1);
            }
        }

        $scope.addData = function() {
            var apiAry = {
                "add": "memberlevelAdd",
                "up": "memberlevelUpdate",
            }

            var api = "add";
            if ($scope.member_level_id) {
                api = "up";
            }

            dataService[apiAry[api]]($scope.member_level).then(function(re2) {
                if (re2.Msg) {
                    helper.myTip("error:" + r.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $state.go("memberlevel.list");
                    });
                }
            });

        }
    }
})();